package mysql

import (
	"doctor-medicine-system/dockerrpc/dockerrpc"

	"gorm.io/gorm"
)

// 药品

type Medicine struct {
	gorm.Model
	Name     string `gorm:"type:varchar(30);not null;comment:药品名称"`
	Style    string `gorm:"type:varchar(30);not null;comment:药品规格"`
	Address  string `gorm:"type:varchar(30);not null;comment:药品生产厂商"`
	Good     string `gorm:"type:varchar(30);not null;comment:药品适应症"`
	Num      string `gorm:"type:varchar(30);not null;comment:药品用法用量"`
	Work     string `gorm:"type:varchar(30);not null;comment:药品注意事项"`
	DockerId int    `gorm:"type:int;not null;comment:医生ID"`
}

// 药品添加

func (m *Medicine) MedicineAdd(db *gorm.DB) error {
	return db.Debug().Create(m).Error
}

// 药品详情

func (m *Medicine) MedicineShow(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Find(m).Error
}

// 药品修改

func (m *Medicine) MedicineUpdate(db *gorm.DB) error {
	return db.Debug().Updates(m).Error
}

// 药品列表

func MedicineList(db *gorm.DB) []*dockerrpc.MedicineList {
	/*
		select
		dockers.id as DockerId,
		dockers.name_code as NameCode,
		dockers.`password` as Password,
		medicines.`name`as MedicineName
		from dockers
		left join medicines on medicines.docker_id = dockers.id
	*/
	var list []*dockerrpc.MedicineList

	db.Model(&Docker{}).
		Select(
			"dockers.id as DockerId",
			"dockers.name_code as NameCode",
			"dockers.`password` as Password",
			"medicines.`name`as MedicineName",
		).
		Joins("left join medicines on medicines.docker_id = dockers.id").
		Debug().Find(&list)

	return list
}
